<?php
namespace Api\Controller;

class NewsController extends ApiController{
    public function __construct(){
        parent::__construct();
        /** 测试不判断登录 modify by zhang 2016 start  **/
        if (!I('mytest')) {
            !api_login() && response(-94);
            !passed_login() && response(-95);
            !only_login() && response(-96);
        }
        /** 测试不判断登录 modify by zhang 2016 end **/
    }
    /**
     *获取资讯栏目列表
     */
    public function getTitle() {
        $result = M('news_type')->select();
        response(1, $result);
    }
    /**
     *获取资讯列表区分栏目
     */
    public function getList() {
        $isHot = I('post.isHot', null);
        $smallTpyeid = M('news_type')->limit(1)->find();
        $type_id = I('post.type_id', $smallTpyeid['id']);//区分资讯栏目
        $page = I('post.page', 1);
        $pagesize = I('post.pagesize', 10);
        $offset = ($page-1) * $pagesize;
        if ($isHot != null) {
            $filters['is_hot'] = $isHot == 1 ? 1 : 0;
        }
        $result['total'] = M('news')->where('type_id='.$type_id)->count() | 0;
        $result['result'] = M('news')->where('type_id='.$type_id)->limit($offset,$pagesize)->order('id DESC')->select();
        foreach ($result['result'] as $key=>$value) {
            $result['result'][$key]['countComment'] = M('news_comment')->where('newsid='.$value['id'])->count();
            $result['result'][$key]['moviePath'] = M('movie')->where('id='.$value['movie_id'])->getField('savepath');
        }
        $result && response(1, $result) && response(-9);
    }
    /**
     *获取资讯详情
     */
    public function getDetail() {
        $id = I('post.id', 0);
        $info = M('news')->find($id);
        $info['moviePath'] = M('movie')->where('id='.$info['movie_id'])->getField('savepath') | '';
        $info || response(-1, null, '参数有误');
        response(1, $info);
    }
    /**
     *获取评论列表
     */
    public function getComment() {
        $newsid = I('post.newsid');
        $result = M('news_comment')->where('newsid='.$newsid)->order('id desc')->select();
        foreach ($result as $key=>$value) {
            $imgid = M('member')->where('uid='.$value['uid'])->getField('avatar');
            $result[$key]['headImg'] = M('picture')->where('id='.$imgid)->getField('path');
            $result[$key]['usetime'] = transfer_time($value['create_time']);
        }
        response(1, $result);
    }
    /**
     *添加评论
     */
    public function addComment() {
        $newsid = I('post.newsid');
        $content = I('post.content');
        $data = array(
            'uid' => API_UID,
            'newsid' => $newsid,
            'content' => $content,
            'create_time' => NOW_TIME
        );
        if (empty($content)) {
            response(-98);
        }
        $result = M('news_comment')->where('uid='.API_UID.'&newsid='.$newsid)->add($data);
        $result && response(1, $result) && response(-9);
    }
    /**
     * 删除评论
     */
    public function deleteComment() {
        $id = I('post.id');
        $commentInfo = M('news_comment')->where('id='.$id)->find();
        if (!$commentInfo) {
            response(-2, '', '评论不存在');
        }
        $result = M('news_comment')->where('id='.$id)->delete();
        $result && response(1, $result) && response(-9);
    }
}
?>